package com.example.demo.mapper;

import com.example.demo.entity.Car;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.DTO.SelectCarDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * <p>
 * 购物车 Mapper 接口
 * </p>
 *
 * @author Mr.sdy
 * @since 2024-06-06
 */
@Mapper
public interface CarMapper extends BaseMapper<Car> {
    @Insert("insert into car(user_id,goods_id,goods_num,create_time) values (#{userId},#{goodsId},#{goodsNum},now())")
    int insertCar(Car car);

    /**
     * @return
     * 跟据用户id和商品id查询购物车
     */
    @Select("SELECT u.user_name,u.nickname,u.phone,tb.* ,tb.goods_num as prod_count,g.goods_name,g.goods_sales,g.goods_address,g.goods_price\n" +
            "        FROM car tb" +
            "        LEFT JOIN user u ON tb.user_id = u.user_id" +
            "        LEFT JOIN goods g ON tb.goods_id = g.goods_id" +
            "        WHERE tb.user_id = #{userId} AND g.flag = 1 OR u.flag =1")
    List<SelectCarDTO> selectCar(Integer car);
}
